package com.example.service;


import cn.hutool.core.util.ObjectUtil;
import com.example.common.Constants;
import com.example.common.Result;
import com.example.common.enums.MemberEnum;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.User;
import com.example.exception.CustomException;
import com.example.mapper.UserMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    /**
     * 添加用户
     * @param user
     */
    public void add(User user) {
        User dbUser = userMapper.selectByUserName(user.getUsername());
        if(ObjectUtil.isNotEmpty(dbUser)){// 判断一下该用户在数据库里有没有，若有抛出异常
            throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
        }
        if(ObjectUtil.isEmpty(user.getPassword())){ // 密码为空的话设置默认密码
            user.setPassword(Constants.USER_DEFAULT_PASSWORD);
        }
        if(ObjectUtil.isEmpty(user.getName())){
            user.setName(user.getUsername());
        }
        user.setMember(MemberEnum.NO.info);// 默认设置为非会员
        user.setRole(RoleEnum.USER.name());// 设置身份角色为用户
        userMapper.insert(user);
    }

    /**
     * 根据id返回用户信息
     * @param id
     * @return
     */
    public User selectById(Integer id){
        User user = userMapper.selectById(id);
        //生成token,防止访问个人中心重新加载数据时token丢失
        String tokenData = user.getId() + "-" + RoleEnum.USER.name();
        String token = TokenUtils.createToken(tokenData, user.getPassword());
        user.setToken(token);
        return user;
    }

    /**
     * 根据id删除用户信息
     * @param id
     */
    public void deleteById(Integer id){
        userMapper.deleteById(id);
    }

    /**
     * 批量删除用户信息
     * @param ids
     */
    public void deleteBatch(List<Integer> ids){
        for(Integer id : ids){
            userMapper.deleteById(id);
        }
    }

    /**
     * 修改用户信息
     * @param user
     */
    public void updateById(User user){
        userMapper.updateById(user);
    }

    /**
     *查询所有用户信息
     * @param user
     * @return
     */
    public List<User> selectAll(User user){
        return userMapper.selectAll(user);
    }

    /**
     * 分页查询
     * @param user
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<User> selectPage(User user, Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum, pageSize); //设置分页参数
        List<User> list = userMapper.selectAll(user); //执行查询操作
        return PageInfo.of(list); //查询结果封装成PageInfo对象返回
    }

    /**
     * 登录
     */
    public Account login(Account account) {
        Account dbUser = userMapper.selectByUserName(account.getUsername());
        if (ObjectUtil.isNull(dbUser)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        if (!account.getPassword().equals(dbUser.getPassword())) {
            throw new CustomException(ResultCodeEnum.USER_ACCOUNT_ERROR);
        }
        String tokenData = dbUser.getId() + "-" + RoleEnum.USER.name();
        String token = TokenUtils.createToken(tokenData, dbUser.getPassword());//生成token
        dbUser.setToken(token);
        return dbUser;
    }

    /**
     * 注册
     * @param account
     */
    public void register(Account account){
        User user = new User();
        BeanUtils.copyProperties(account, user);
        add(user);
    }

    /**
     * 修改密码
     * @param account
     */
    public void updatePassword(Account account){
        User dbUser = userMapper.selectByUserName(account.getUsername());
        if(ObjectUtil.isEmpty(dbUser)){
            throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
        }
        if(!account.getPassword().equals(dbUser.getPassword())){
            throw new CustomException(ResultCodeEnum.PARAM_PASSWORD_ERROR);
        }
        dbUser.setPassword(account.getNewPassword());
        userMapper.updateById(dbUser);
    }

    /**
     * 充值
     * @param account
     */
    public void recharge(Double account){
        //获取当前账户
        Account currentUser = TokenUtils.getCurrentUser();
        //获取用户的详细信息
        User user = userMapper.selectById(currentUser.getId());

        user.setAccount(user.getAccount() + account);
        //是否充值一次性满500
        if(account >= 500){
            user.setMember(MemberEnum.YES.info);
        }

        userMapper.updateById(user);
    }
}
